package com.ffcs.cn.action.scene;

import com.ffcs.cn.DevelopProperties;
import com.supermap.data.*;
import com.supermap.desktop.core.Application;
import com.supermap.desktop.core.Interface.IBaseItem;
import com.supermap.desktop.core.Interface.IFormScene;
import com.supermap.desktop.core.enums.LengthUnit;
import com.supermap.desktop.core.implement.CtrlAction;
import com.supermap.desktop.core.utilties.FontUtilities;
import com.supermap.desktop.realspaceview.FormScene;
import com.supermap.ui.Action3D;
import com.supermap.ui.Tracked3DListener;
import com.supermap.ui.Tracking3DListener;

import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.text.MessageFormat;

/**
 * 计算空间距离
 *
  */
public class CtrlActionMeasureDistance extends CtrlAction {

	private static final String CURRENT_INFO = "KEY_CURRENT_INFO";

	public CtrlActionMeasureDistance(IBaseItem caller) {
		super(caller);
	}

	/**
	 * 地图绘制的监听器，将当前距离信息实时显示，并放置到TrackingLayer上
	 */
	private final Tracking3DListener tracking3DListener = e -> {
		IFormScene formScene = ((FormScene) Application.getActiveApplication().getActiveForm());
		GeoText3D currentInfo;
		int index = formScene.getSceneControl().getScene().getTrackingLayer().indexOf(CURRENT_INFO);
		if (index == -1) {
			// 设置文本样式
			TextStyle textStyle = new TextStyle();
			textStyle.setFontHeight(5);
			textStyle.setAlignment(TextAlignment.BOTTOMLEFT);
			textStyle.setBackColor(Color.BLACK);
			textStyle.setForeColor(Color.red);
			textStyle.setOutline(true);
			textStyle.setFontName(FontUtilities.getDefaultFont().getFontName());

			TextPart3D textPart3D = new TextPart3D();
			textPart3D.setAnchorPoint(new Point3D(0, 0, 0));

			GeoStyle3D style3D = new GeoStyle3D();
			style3D.setAltitudeMode(AltitudeMode.ABSOLUTE);

			currentInfo = new GeoText3D(textPart3D, textStyle);
			currentInfo.setStyle3D(style3D);
			index = formScene.getSceneControl().getScene().getTrackingLayer().add(currentInfo, CURRENT_INFO);
		} else {
			currentInfo = ((GeoText3D) formScene.getSceneControl().getScene().getTrackingLayer().get(index));
		}
		// 总距离
		double totalDistance = LengthUnit.ConvertDistance(LengthUnit.METER.getUnit(), formScene.getLengthUnit().getUnit(), e.getTotalLength());
		currentInfo.getPart(0).setText(MessageFormat.format(DevelopProperties.getString("String_TotalDistance"), totalDistance));
		currentInfo.getPart(0).setX(e.getX());
		currentInfo.getPart(0).setY(e.getY());
		currentInfo.getPart(0).setZ(e.getZ());

		formScene.getSceneControl().getScene().getTrackingLayer().set(index, currentInfo);
	};

	private final MouseAdapter mouseAdapter = new MouseAdapter() {
		@Override
		public void mouseReleased(MouseEvent e) {
			if (e.getButton() == MouseEvent.BUTTON3) {
				IFormScene formScene = ((IFormScene) Application.getActiveApplication().getActiveForm());
				formScene.getSceneControl().removeTrackingListener(tracking3DListener);
				formScene.getSceneControl().removeMouseListener(mouseAdapter);
				formScene.getSceneControl().removeTrackedListener(tracked3DListener);
				formScene.showPopupMenu();
				formScene.getSceneControl().setAction(Action3D.PAN);
			}
		}
	};

	/**
	 * 绘制结束的监听器
	 */
	private final Tracked3DListener tracked3DListener = e -> {
		IFormScene formScene = ((IFormScene) Application.getActiveApplication().getActiveForm());
		// 获取当前为止绘制的总路程几何对象
		GeoLine3D geoLine3D = ((GeoLine3D) e.getGeometry());
		if (geoLine3D != null) {
			// 绘制整个路径
			for (int i = 0; i < geoLine3D.getPartCount(); i++) {
				Point3Ds points = geoLine3D.getPart(i);
				for (int j = 0; j < points.getCount(); j++) {
					GeoPoint3D geoPoint3D = new GeoPoint3D(points.getItem(j));
					geoPoint3D.setStyle3D(new GeoStyle3D());
					geoPoint3D.getStyle3D().setAltitudeMode(AltitudeMode.ABSOLUTE);
					formScene.getSceneControl().getScene().getTrackingLayer().add(geoPoint3D, "TRACKING_OBJECT_NAME");
				}
			}
			formScene.getSceneControl().getScene().getTrackingLayer().add(geoLine3D, "TRACKING_OBJECT_NAME");
			GeoText3D geoText3D = ((GeoText3D) formScene.getSceneControl().getScene().getTrackingLayer()
					.get(formScene.getSceneControl().getScene().getTrackingLayer().indexOf(CURRENT_INFO)).clone());
			formScene.getSceneControl().getScene().getTrackingLayer().remove(formScene.getSceneControl().getScene().getTrackingLayer().indexOf(CURRENT_INFO));
			formScene.getSceneControl().getScene().getTrackingLayer().add(geoText3D, "TRACKING_OBJECT_NAME");
		}
	};

	@Override
	protected void run() {
		if (!(Application.getActiveApplication().getActiveForm() instanceof IFormScene)) {
			return;
		}
		IFormScene formScene = ((IFormScene) Application.getActiveApplication().getActiveForm());
		// 为当前场景窗口设置三维操作状态类型
		formScene.getSceneControl().setAction(Action3D.MeasureDistance);
		formScene.getSceneControl().removeTrackingListener(tracking3DListener);
		formScene.getSceneControl().addTrackingListener(tracking3DListener);
		formScene.getSceneControl().removeTrackedListener(tracked3DListener);
		formScene.getSceneControl().addTrackedListener(tracked3DListener);
		formScene.getSceneControl().removeMouseListener(mouseAdapter);
		formScene.getSceneControl().addMouseListener(mouseAdapter);
		formScene.dontShowPopupMenu();
	}

	@Override
	public boolean enable() {
		return Application.getActiveApplication().getActiveForm() instanceof IFormScene;
	}
}
